package com.xms.ribbonbutton.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xms.core.model.PagedList;
import com.xms.ribbonbutton.entity.RibbonButton;
import com.xms.ribbonbutton.service.IDefaultButtonProvider;
import com.xms.ribbonbutton.service.IRibbonButtonFinderService;
import com.xms.schema.core.EntityMask;
import com.xms.utils.StringUtil;
import com.xms.utils.UUIDUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author migo
 * @since 2021-08-29
 */
@RestController
@RequestMapping("{org}/api/ribbonbutton")
public class RibbonButtonFinderController extends ApiBaseController {
    private IRibbonButtonFinderService _ribbonButtonFinderService;
    private IDefaultButtonProvider _defaultButtonProvider;
    @Autowired
    public RibbonButtonFinderController(IRibbonButtonFinderService ribbonButtonFinderService,
                                        IDefaultButtonProvider defaultButtonProvider){
        _ribbonButtonFinderService = ribbonButtonFinderService;
        _defaultButtonProvider = defaultButtonProvider;
    }

    @ApiOperation("查询实体按钮")
    @GetMapping("list")
    public JsonResultObject list(@RequestParam(value="entityid", required = true) UUID entityId,
                                @RequestParam(value="showarea", required = false) Integer showarea,
                                @RequestParam(value="label", required = false) String label,
                                @RequestParam(value="getall", required = false) Boolean getall,
                                @RequestParam(value="page", required = false) Integer page,
                                @RequestParam(value="pagesize", required = false) Integer pagesize,
                                @RequestParam(value="sortby", required = false) String sortby,
                                @RequestParam(value="sortdirection", required = false) String sortdirection)
    {
        if (UUIDUtil.isEmpty(entityId))
        {
            return JNotSpecifiedParams("entityid");
        }
        LambdaQueryWrapper<RibbonButton> lambdaQueryWrapper = getSortedQueryWrapper(sortby, sortdirection);
        lambdaQueryWrapper.eq(RibbonButton::getEntityid, entityId);
        if (StringUtil.notEmpty(label))
        {
            lambdaQueryWrapper.like(RibbonButton::getLabel, label);
        }
        if (showarea != null && showarea > 0)
        {
            lambdaQueryWrapper.eq(RibbonButton::getShowarea, showarea);
        }
        if (getall != null && getall) {
            return JOk(_ribbonButtonFinderService.list(lambdaQueryWrapper));
        }
        else if(page == null || pagesize == null){
            return JNotSpecifiedParams("page","pagesize");
        }
        page = (page <= 0 ? 1 : page);
        pagesize = (pagesize <= 0 ? 10 : pagesize);
        IPage<RibbonButton> datas = _ribbonButtonFinderService.page(new Page<>(page, pagesize), lambdaQueryWrapper);
        PagedList<RibbonButton> result = new PagedList<>(datas.getRecords(), page, pagesize, datas.getTotal(), datas.getPages());

        return JOk(result);
    }

    /**
     *
     * @param id 按钮id，多个以英文逗号分隔
     * @return
     */
    @ApiOperation("查询按钮")
    @RequestMapping(value = "getbyids/{id}", method = RequestMethod.GET)
    public JsonResultObject getByIds(@PathVariable(value="id", required = true) String id)
    {
        if (StringUtil.notEmpty(id))
        {
            String[] buttonId = StringUtil.splitSafe(",", id);
            List<UUID> ids = Arrays.stream(buttonId).map(x->UUIDUtil.get(x)).collect(Collectors.toList());
            List<RibbonButton> result = _ribbonButtonFinderService.getByIds(ids);
            return JOk(result);
        }
        return JOk();
    }

    @ApiOperation("查询按钮")
    @RequestMapping(value = "getbyid/{id}", method = RequestMethod.GET)
    public JsonResultObject getById(@PathVariable(value="id", required = true) UUID id)
    {
        return JOk(_ribbonButtonFinderService.getById(id));
    }

    @ApiOperation("查询所有按钮")
    @RequestMapping(value = "getall", method = RequestMethod.GET)
    public JsonResultObject getAll()
    {
        List<RibbonButton> result = _ribbonButtonFinderService.list(new LambdaQueryWrapper<RibbonButton>().eq(RibbonButton::getStatecode, true));
        return JOk(result);
    }

    @ApiOperation("查询系统标准按钮")
    @RequestMapping(value = "SystemButtons/{entitymask}", method = RequestMethod.GET)
    public JsonResultObject GetSystemButtons(@PathVariable(value="entitymask", required = true) EntityMask entityMask)
    {
        return JOk(_defaultButtonProvider.get(entityMask.value()));
    }
}
